package com.db.client.demo;

import com.db.client.Client;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author nulls.narik
 */
public class DemoClient {

    private static int search(String[] args, String arg) {
        int index = 0;
        for (String value : args) {
            if (arg.equals(value))
                return index;
            index++;
        }
        return -1;
    }

    private static final String ID_KEY = "-id";
    private static final String PORT_KEY = "-port";
    private static final String ADDRESS_KEY = "-address";
    private static final String TYPE_AGGREGATION_KEY = "-type";

    public static void main(String[] args) throws UnknownHostException {
        String ID = "testClient";
        int port = 40000;
        InetAddress address = InetAddress.getLocalHost();
        int type = 3;

        int index = search(args, PORT_KEY);
        if (index >= 0 && index < args.length - 1) {
            try {
                port = Integer.valueOf(args[index + 1]);
            } catch (NumberFormatException exception) {
                port = 40000;
            }
        }

        index = search(args, ADDRESS_KEY);
        if (index >= 0 && index < args.length - 1) {
            try {
                address = InetAddress.getByName(args[index + 1]);
            } catch (NumberFormatException exception) {
                address = InetAddress.getLocalHost();
            }
        }

        index = search(args, TYPE_AGGREGATION_KEY);
        if (index >= 0 && index < args.length - 1) {
            try {
                type = Integer.valueOf(args[index + 1]);
            } catch (NumberFormatException exception) {
                type = 3;
            }
        }

        index = search(args, ID_KEY);
        if (index >= 0 && index < args.length - 1) {
            ID = args[index + 1];
        }

        Client client = new Client(ID, type);
        client.connect(address, port);
        while (true) {

        }
    }

}
